package net.neptech.controller;


import net.neptech.model.ipc.InteractionRecord;
import net.neptech.service.SessionService;
import net.neptech.service.SensitiveWordService;
import net.neptech.vm.ResponseResult;
import net.neptech.vm.enums.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author HuangZhengmin
 * @desc
 * @Date 2025/8/19 21:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/ipc")
public class IpcController {

  @Autowired
  private SessionService sessionService;

  @Autowired
  private SensitiveWordService sensitiveWordService;

  /**
   * 开始新的问诊会话
   */
  @PostMapping("/start")
  public ResponseResult<Map<String, Object>> startConsultation(@RequestParam Long patientId) {
    try {
      String sessionId = sessionService.createSession(patientId);
      Map<String, Object> result = new HashMap<>();
      result.put("sessionId", sessionId);
      result.put("message", "问诊会话已创建，请描述您的症状");
      return ResponseResult.success(result);
    } catch (Exception e) {
      return ResponseResult.fail(500, e.getMessage());
    }
  }

  /**
   * 处理用户输入，推进问诊流程
   */
  @PostMapping("/process/{sessionId}/{patientId}")
  public ResponseResult<Map<String, Object>> processInput(
          @PathVariable(required = false) String sessionId,
          @PathVariable Long patientId,
          @RequestBody Map<String, String> input) {

    try {
      String userInput = input.getOrDefault("input", "");

      // 检查敏感词
      if (sensitiveWordService.containsSensitiveWords(userInput)) {
        return ResponseResult.fail(400, "输入包含敏感词，请调整后重试");
      }

      Map<String, Object> result = sessionService.processUserInput(sessionId, patientId, userInput);
      return ResponseResult.success(result);
    } catch (IllegalArgumentException e) {
      return ResponseResult.fail(400, e.getMessage());
    } catch (Exception e) {
      return ResponseResult.fail(500, e.getMessage());
    }
  }

  /**
   * 获取会话历史
   */
  @GetMapping("/history/{sessionId}")
  public ResponseResult<Map<String, Object>> getSessionHistory(@PathVariable String sessionId) {
    try {
      List<InteractionRecord> history = sessionService.getInteractionHistory(sessionId);
      Map<String, Object> session = sessionService.getSession(sessionId);

      Map<String, Object> result = new HashMap<>();
      result.put("sessionId", sessionId);
      result.put("sessionInfo", session);
      result.put("history", history);

      return ResponseResult.success(result);
    } catch (IllegalArgumentException e) {
      return ResponseResult.fail(400, e.getMessage());
    } catch (Exception e) {
      return ResponseResult.fail(500, e.getMessage());
    }
  }

  /**
   * 获取患者的所有会话ids
   */
  @GetMapping("/patient/{patientId}/sessions")
  public ResponseResult<Map<String, Object>> getPatientSessions(@PathVariable Long patientId) {
    try {
      List<String> sessionIds = sessionService.getPatientSessions(patientId);
      Map<String, Object> result = new HashMap<>();
      result.put("patientId", patientId);
      result.put("sessionCount", sessionIds.size());
      result.put("sessions", sessionIds);

      return ResponseResult.success(result);
    } catch (Exception e) {
      return ResponseResult.fail(500, e.getMessage());
    }
  }

  /**
   * 敏感词管理 - 添加敏感词
   */
  @PostMapping("/sensitive-word")
  public ResponseResult<Void> addSensitiveWord(
          @RequestParam String word,
          @RequestParam String category) {
    try {
      sensitiveWordService.addSensitiveWord(word, category);
      return ResponseResult.success();
    } catch (Exception e) {
      return ResponseResult.fail(500, e.getMessage());
    }
  }

  /**
   * 敏感词管理 - 移除敏感词
   */
  @DeleteMapping("/sensitive-word")
  public ResponseResult<Void> removeSensitiveWord(@RequestParam String word) {
    try {
      sensitiveWordService.removeSensitiveWord(word);
      return ResponseResult.success();
    } catch (Exception e) {
      return ResponseResult.fail(500, e.getMessage());
    }
  }
}
